Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int** scanArray(int rows, int cols);
void freeArray(int** arr, int rows);
int possible(int** curGame, int slots, int colors, int moves, int k, int* guess);
int validGuess(int** curGame, int slots, int moves, int* guess);
int main (){
int games, slots, colors, moves;
int i;
scanf("%d", &games);
for (i=0; i <games; i++){
scanf("%d %d %d", &slots, &colors, &moves);
int** curGame= scanArray(moves, slots+2);
int* guess = malloc(sizeof(int)*slots);
printf("%d\n", possible(curGame, slots, colors, moves, 0, guess));
free(guess);
freeArray(curGame, moves);
}
return 0;
}
int** scanArray(int rows, int cols){
int** grid = malloc(sizeof(int*) * rows);
int i, j;
for (i=0; i<rows; i++){
grid[i] = malloc(sizeof(int) * cols);
for (j=0; j<cols; j++)
scanf("%d", &grid[i][j]);
}
return grid;
}
void freeArray(int** arr, int rows){
int i;
for (i=0; i<rows; i++){
free(arr[i]);
}
free(arr);
}
int possible(int** curGame, int slots, int colors, int moves, int k, int* guess){
int i, sum=0;
if(k == slots)
return validGuess(curGame, slots, moves, guess);
else {
for(i=0; i<colors; i++){
guess[k] = i;
sum+= possible(curGame, slots, colors, moves, k+1, guess);
}
}
return sum;
}
int validGuess(int** curGame, int slots, int moves, int* guess){
int black, white, i, j, k;
int* used = (int*)calloc(slots, sizeof(int));
for (i=0; i<moves; i++){
black= 0;
white= 0;
//for (j=0; j<slots; j++)
//used[j]= 0;
for (j=0; j<slots; j++){
if (curGame[i][j] == guess[j]){
black++;
used[j] = 1;
}
}
if (black != curGame[i][slots+1])
return 0;
for (j=0; j<slots; j++){
if (used[j] == 1){
continue;
}
for (k=0; k<slots; k++){
if (used[k] == 1){
continue;
}
if (curGame[i][j] == guess[k]){
white++;
break;
}
}
if (white != curGame[i][slots+2])
return 0;
}
}
return 1;
}
and here is the actual problem that the Professor typed out :Your task will be to analyze a partially completed game and calculate the number of possiblecolor combinations that are completely consistent with all of the given information. Due to thefact that the manufacturer may release different versions of the game with a different number ofpossible colors and a different number of possible slots, your program should work where thesetwo values are given as variables